use model_graph_types::application::agent::{AgentChatRequestMessage, AgentChatResponseMessage};
use model_graph_types::database::DataSource;
use tauri::{command, AppHandle, Runtime, State, Window};

use crate::handlers::ContextState;
use crate::handlers::Result;

use container_application::get_application_handler;

#[command]
pub async fn app_agent_chat_send<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
    identifier: String,
    is_installed: bool,
    message: AgentChatRequestMessage,
) -> Result<AgentChatResponseMessage> {
    //获取到数据源
    let _context = state.context.clone();

    let handler = get_application_handler(&identifier, is_installed).await?;
    let response_message = handler.lock().await.agent_chat_request(message).await?;

    Ok(response_message)
}

#[command]
pub async fn app_datasource<R: Runtime>(
    _app: AppHandle<R>,
    _window: Window<R>,
    state: State<'_, ContextState>,
    identifier: String,
    is_installed: bool,
) -> Result<Option<DataSource>> {
    //获取到数据源
    let _context = state.context.clone();

    let handler = get_application_handler(&identifier, is_installed).await?;
    let response_message = handler.lock().await.application_datasource().await?;

    Ok(response_message)
}
